//
//  PDFDocumentPreviewView.m
//  Digital Seamonsters
//
//  Created by Adam Cue on 2/6/11.
//  Copyright Adam Cue 2011.
//
//  This program is licensed and distributed under the terms of the GNU General Public License.  
//	The text for the license can be found in the license.txt file in this project.
//

#import "PDFDocumentPreviewView.h"

@interface PDFDocumentPreviewView (Private)
#pragma mark UserActions
- (void)tapped;
- (void)held;
#pragma mark HelperMethods
- (void)deleteSelf;
- (void)alterView;
@end


@implementation PDFDocumentPreviewView

@synthesize tapGesture = _tapGesture;
@synthesize longPressGesture = _longPressGesture;
@synthesize delegate;
@synthesize index;
@synthesize saved;

static CGFloat kDefaultReflectionFraction = 0.65;
static CGFloat kDefaultReflectionOpacity = 0.40;

- (id)initWithFrame:(CGRect)frame index:(int)selectedIndex{
    self = [super initWithFrame:frame];
    if (self) {
		self.index = selectedIndex;
    }
    return self;
}

- (void)dealloc{
   self.tapGesture = nil;
   self.longPressGesture = nil;
   self.delegate = nil;
   
   [super dealloc];
}

#pragma mark UserActions
- (void)tapped{
	[delegate selected:index withImage:[self image]];
}

- (void)held{
	if(saved == YES && self.longPressGesture.state == UIGestureRecognizerStateBegan){
		[self showCloseButton];
		[delegate shaking];
	}
}

#pragma mark HelperMethods
- (void)deleteSelf{
	UIAlertView *deleteAlert = [[[UIAlertView alloc] initWithTitle:@"Delete Reading" 
                                                          message:@"Do you want to delete this reading?" 
                                                         delegate:self 
                                                cancelButtonTitle:@"No" 
                                                otherButtonTitles:@"Yes", nil] autorelease];
	[deleteAlert show];
}

- (void)alterView{
   if(_tapGesture){
      [self removeGestureRecognizer:self.tapGesture];
      self.tapGesture = nil;
   }
   if(_longPressGesture){
      [self removeGestureRecognizer:self.longPressGesture];
      self.longPressGesture = nil;
   }
	_tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped)];
	[self addGestureRecognizer:self.tapGesture];
	_longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(held)];
	[self addGestureRecognizer:self.longPressGesture];
	UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
	[closeButton setImage:[UIImage imageNamed:@"closebox"] forState:UIControlStateNormal];
	closeButton.frame = CGRectMake(2, 2, 35, 35);
	closeButton.hidden = YES;
	[closeButton addTarget:self action:@selector(deleteSelf) forControlEvents:UIControlEventTouchUpInside];
	[self addSubview:closeButton];
}

#pragma mark ExternalMethods
- (void)showCloseButton{
	[self removeGestureRecognizer:self.tapGesture];
	[self removeGestureRecognizer:self.longPressGesture];
	for(UIView *view in self.subviews){
		view.hidden = NO;
	}
}

- (void)doneShaking{
   [self addGestureRecognizer:self.tapGesture];
   [self addGestureRecognizer:self.longPressGesture];
}

#pragma mark AsyncImageView Overrides
- (void)connectionDidFinishLoading:(NSURLConnection *)theConnection{
   //so self data now has the complete image 
	[connection release];
	connection=nil;
	for(UIView *view in self.subviews){
		[view removeFromSuperview];
	}
	//make an image view for the image
	NSData *image = UIImageJPEGRepresentation([UIImage imageWithData:data], 1.0);
	UIImageView* imageView = [[[UIImageView alloc] initWithImage:[UIImage imageWithData:image]] autorelease];
	//make sizing choices based on your needs, experiment with these. maybe not all the calls below are needed.
	imageView.contentMode = UIViewContentModeScaleAspectFill;
	imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight );
	[self addSubview:imageView];
   imageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height / 2);
   
   NSUInteger reflectionHeight = [self image].size.height * kDefaultReflectionFraction;
	
   UIImageView * reflectedImage = [[[UIImageView alloc] initWithFrame:CGRectMake(0, imageView.frame.size.height, imageView.frame.size.width, reflectionHeight)] autorelease];
	reflectedImage.image = [self reflectedImage:imageView.image withWidth:imageView.width andHeight:reflectionHeight];
	reflectedImage.alpha = kDefaultReflectionOpacity;
   [self addSubview:reflectedImage];
	[data release]; //don't need this any more, its in the UIImageView now
	data=nil;
	[self alterView];
}

- (void)setDisplayImage:(UIImage *)selectedImage{
	for(UIView *view in self.subviews){
		[view removeFromSuperview];
	}
	//make an image view for the image
	UIImageView* imageView = [[[UIImageView alloc] initWithImage:selectedImage] autorelease];
	//make sizing choices based on your needs, experiment with these. maybe not all the calls below are needed.
	imageView.contentMode = UIViewContentModeScaleAspectFill;
	imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight );
	[self addSubview:imageView];
	imageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height / 2);
	NSUInteger reflectionHeight = [self image].size.height * kDefaultReflectionFraction;
	
   UIImageView * reflectedImage = [[[UIImageView alloc] initWithFrame:CGRectMake(0, imageView.frame.size.height, imageView.frame.size.width, reflectionHeight)] autorelease];
	reflectedImage.image = [self reflectedImage:imageView.image withWidth:imageView.width andHeight:reflectionHeight];
	reflectedImage.alpha = kDefaultReflectionOpacity;
   [self addSubview:reflectedImage];
   [self alterView];
}

#pragma mark UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
	if(buttonIndex != 0){
		[delegate deleted:index];
	}
}

@end
